package com.zzyl.nursing.service.impl;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.mapper.FamilyMemberMapper;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.service.WeChatService;
import com.zzyl.nursing.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 老人家属Service业务层处理
 *
 * @author FuJiaWei
 * @date 2025-05-29
 */
@Service
public class FamilyMemberServiceImpl extends ServiceImpl<FamilyMemberMapper, FamilyMember> implements IFamilyMemberService {
    @Autowired
    private FamilyMemberMapper familyMemberMapper;
    @Autowired
    private WeChatService weChatService;
    @Autowired
    private TokenService tokenService;

    static List<String> DEFAULT_NICKNAME_PREFIX = ListUtil.of(
            "生活更美好",
            "大桔大利",
            "日富一日",
            "好柿开花",
            "柿柿如意",
            "一椰暴富",
            "大柚所为",
            "杨梅吐气",
            "天生荔枝"
    );


    /**
     * 查询老人家属
     *
     * @param id 老人家属主键
     * @return 老人家属
     */
    @Override
    public FamilyMember selectFamilyMemberById(Long id) {
        return getById(id);
    }

    /**
     * 查询老人家属列表
     *
     * @param familyMember 老人家属
     * @return 老人家属
     */
    @Override
    public List<FamilyMember> selectFamilyMemberList(FamilyMember familyMember) {
        return familyMemberMapper.selectFamilyMemberList(familyMember);
    }

    /**
     * 新增老人家属
     *
     * @param familyMember 老人家属
     * @return 结果
     */
    @Override
    public int insertFamilyMember(FamilyMember familyMember) {
        return save(familyMember) ? 1 : 0;
    }

    /**
     * 修改老人家属
     *
     * @param familyMember 老人家属
     * @return 结果
     */
    @Override
    public int updateFamilyMember(FamilyMember familyMember) {
        return updateById(familyMember) ? 1 : 0;
    }

    /**
     * 批量删除老人家属
     *
     * @param ids 需要删除的老人家属主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除老人家属信息
     *
     * @param id 老人家属主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 小程序登入
     *
     * @param userLoginRequestDto
     * @return
     */
    @Override
    public LoginVo login(UserLoginRequestDto userLoginRequestDto) {
        //获取openId
        String openId = weChatService.getOpenId(userLoginRequestDto.getCode());
        //获取手机号
        String phone = weChatService.getPhone(userLoginRequestDto.getPhoneCode());
        //查询数据库客户信息
        FamilyMember memberInDb = lambdaQuery().eq(FamilyMember::getOpenId, openId).one();
        LoginVo loginVo = new LoginVo();
        new FamilyMember();
        FamilyMember member = new FamilyMember();
        //判断数据库是否有该家属
        if (null == memberInDb) {
            //没有则新建一个保存
            member = FamilyMember.builder()
                    .openId(openId)
                    .phone(phone)
                    .name(getName(phone))
                    .build();
            save(member);
            //设置登入名称
            loginVo.setNickName(member.getName());
        } else {
            //设置登入名称
            loginVo.setNickName(memberInDb.getName());
            //有则判断手机号是否相同
            member.setId(memberInDb.getId());
            if (!phone.equals(memberInDb.getPhone())) {
                //不同则更新手机号
                member.setPhone(phone);
                updateById(member);
            }
        }
        Map<String, Object> claims = new HashMap<>();
        claims.put("userID", member.getId());
        claims.put("nickName", loginVo.getNickName());
        String token = tokenService.createToken(claims);
        loginVo.setToken(token);
        return loginVo;
    }

    private String getName(String phone) {
        return DEFAULT_NICKNAME_PREFIX.get((int) (Math.random() * DEFAULT_NICKNAME_PREFIX.size())) + phone.substring(7);
    }
}
